﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data;
using System.Data.SqlClient;


namespace cvn.data
{
    public class SqlDataHelper
    {
        private SqlDatabase dbinstance;
        public SqlDataHelper()
        {
            
            dbinstance = new SqlDatabase(SqlConString.ConnString);       
          
        }
        
        
        /// <summary>
        /// Excute a SQL update/delete statement against the database and retuns row(s) updated.
        /// </summary>
        /// <param name="Sql"></param>
        /// <returns></returns>
        public int updateTableRows(string Sql)
        {
           
            int rowsAffected = dbinstance.ExecuteNonQuery(CommandType.Text, Sql);
            return rowsAffected;
           
        }
        
        /// <summary>
        /// Executes a stored procedure and retuns row(s) updated.
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public int updateTableRows(string procName,params object[] values)
        {
            int rowsAffected = dbinstance.ExecuteNonQuery(procName, values);
            return rowsAffected;
        }

        public int GetFirstColumnAsInt(string Sql)
        {
            int count = (int) dbinstance.ExecuteScalar(CommandType.Text, Sql);
            return count;
        }

        public int GetFirstColumnAsInt(string procName, params object[] values)
        {
            int count = (int)dbinstance.ExecuteScalar(procName, values);
            return count;
        }

        public string GetFirstColumnAsText(string Sql)
        {
            string value = (string)dbinstance.ExecuteScalar(CommandType.Text, Sql);
            return value;
        }

        public string GetFirstColumnAsText(string procName, params object[] values)
        {
            string value = (string)dbinstance.ExecuteScalar(procName, values);
            return value;
        }
    }
}
